//
//  WDWorderOrdVehDetailReplacePartView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/6.
//

#import "WDWorderOrdVehDetailReplacePartView.h"
#import "WDWorderOrdMainVehSingleImageView.h"
#import "WDInspectUploadModel.h"
#import "HZPhotoBrowser.h"

@interface WDWorderOrdVehDetailReplacePartView ()

/// 备注图片
@property (nonatomic, strong) UILabel *remarkImgTitleLabel;

/// 新旧件对比照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *contrastImgView;

/// 车辆换件前照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *beforeImgView;

/// 车辆换件后照片
@property (nonatomic, strong) WDWorderOrdMainVehSingleImageView *afterImgView;



/// 备注图片
- (void)initWithRemarkImgTitleLabel;

/// 新旧件对比照片
- (void)initWithContrastImgView;

/// 车辆换件前照片
- (void)initWithBeforeImgView;

/// 车辆换件后照片
- (void)initWithAfterImgView;


@end

@implementation WDWorderOrdVehDetailReplacePartView

- (void)initWithSubView {
    [super initWithSubView];
    
    
}

/// 备注图片
- (void)initWithRemarkImgTitleLabel {
    if (!_remarkImgTitleLabel) {
        CGRect frame = CGRectMake(20, 0, self.width - 40, 18);
        _remarkImgTitleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.text = @"备注图片";
        }];
    }
}

/// 新旧件对比照片
- (void)initWithContrastImgView {
    if (!_contrastImgView) {
        float imageWidth = (kScreenW - 20 * 2 - 16 * 2) / 3.0;
        float imageHeight = imageWidth / 1.42857;
        float imageTitleHeight = imageHeight + 5 + 18;
        CGRect frame = CGRectMake(20, self.remarkImgTitleLabel.bottom + 12, imageWidth, imageTitleHeight);
        _contrastImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_contrastImgView];
        @weakify(self);
        _contrastImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.dataList.count > 0) {
        WDInspectUploadModel *model = self.dataList.firstObject;
        [self.contrastImgView updateWithViewTitle:@"新旧件对比照片" imageUrl:model.uploadUrl];
    }
}

/// 车辆换件前照片
- (void)initWithBeforeImgView {
    if (!_beforeImgView) {
        CGRect frame = self.contrastImgView.frame;
        frame.origin.x = self.contrastImgView.right + 16;
        _beforeImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_beforeImgView];
        @weakify(self);
        _beforeImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.dataList.count > 1) {
        WDInspectUploadModel *model = self.dataList[1];
        [self.beforeImgView updateWithViewTitle:@"车辆换件前照片" imageUrl:model.uploadUrl];
    }
}

/// 车辆换件后照片
- (void)initWithAfterImgView {
    if (!_afterImgView) {
        CGRect frame = self.contrastImgView.frame;
        frame.origin.x = self.beforeImgView.right + 16;
        _afterImgView = [[WDWorderOrdMainVehSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_afterImgView];
        @weakify(self);
        _afterImgView.touchShowPicBlock = ^(UIImageView * _Nonnull imageView, NSString * _Nonnull imageUrl) {
            @strongify(self);
            [self browseImageView:0 view:imageView picList:@[imageUrl]];
        };
    }
    if (self.dataList.count == 3) {
        WDInspectUploadModel *model = self.dataList[2];
        [self.afterImgView updateWithViewTitle:@"车辆换件后照片" imageUrl:model.uploadUrl];
    }
}

- (void)browseImageView:(NSInteger)index view:(UIImageView *)view picList:(NSArray *)picList {
    // 图片预览
    HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
    browser.isNeedLandscape = NO;
    browser.imageArray = [picList copy];
    browser.currentImageIndex = (int)index;
    browser.imageCount = picList.count;
    if(picList.count == 1)//如果是一张图片tag 是200
    {
        browser.sourceImagesContainerView = view;
        browser.startTag = 200;
    }
    else//如果是多张图片tag 从100开始
    {
        browser.startTag = 100;
        browser.sourceImagesContainerView = view;
    }
    [browser show];
}

- (void)updateWithViewData:(id)data {
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:data];
    
    /// 备注图片
    [self initWithRemarkImgTitleLabel];

    /// 新旧件对比照片
    [self initWithContrastImgView];

    /// 车辆换件前照片
    [self initWithBeforeImgView];

    /// 车辆换件后照片
    [self initWithAfterImgView];

}

@end
